package com.remeberword;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.remeberword.db.Word;

import java.util.List;

/**
 * Created by arch on 2017/12/9.
 */

public class WordAdapter extends RecyclerView.Adapter<WordAdapter.ViewHolder> {

    protected List<Word> wordList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        private View wordView;

        TextView wordEnglish;

        boolean isEnglish = true;
//        TextView wordChinese;

        public ViewHolder(View view) {
            super(view);
            wordView = view;
            wordEnglish = (TextView) view.findViewById(R.id.word_english);
            isEnglish = true;
//            wordChinese = (TextView) view.findViewById(R.id.word_chinese);
        }
    }

    public WordAdapter(List<Word> wordList) {
        this.wordList = wordList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.word_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.wordView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Word word = wordList.get(position);
                if (holder.isEnglish) {
                    holder.wordEnglish.setText(word.getChinese());
                    holder.isEnglish = false;
                } else {
                    holder.wordEnglish.setText(word.getEnglish());
                    holder.isEnglish = true;
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Word word = wordList.get(position);
        holder.wordEnglish.setText(word.getEnglish());
//        holder.wordChinese.setText(word.getChinese());
    }

    @Override
    public int getItemCount() {
        return wordList.size();
    }

/*
    public WordAdapter(@NonNull Context context, int textViewResourceId, @NonNull List<Word> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Word word = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.wordEnglish = (TextView) view.findViewById(R.id.word_english);
            viewHolder.wordChinese = (TextView) view.findViewById(R.id.word_chinese);
            viewHolder.wordEnglish.setText(word.getEnglish());
            viewHolder.wordChinese.setText(word.getChinese());
            view.setTag(viewHolder);
        } else
            view = convertView;
        return view;
    }*/
}
